package dk.iha;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.os.Environment;

public class Utility {
	/**
	 * Writes a file to a specified location on the external storege (SDCard)
	 * @param filelocation
	 * @param fileName
	 * @param data
	 * @param append - if true; then data is appended to the file
	 * @return success
	 */
	@SuppressWarnings("resource")
	public static boolean writeFile(String filelocation, String fileName, String data, boolean append){
		File file = null;
		File root = Environment.getExternalStorageDirectory();
		if (root.canWrite()) {
			File dir = new File(filelocation);
			dir.mkdirs();
			file = new File(dir, fileName);
			FileOutputStream out = null;
			try {
				out = new FileOutputStream(file, append);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				return false;
			}
			try {
				out.write(data.getBytes());
			} catch (IOException e) {
				e.printStackTrace();
				return false;
			}
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
				return false;
			}
		}
		return true;
	}

	/**
	 * 	runs trough basefolder on sdcard and checks which folder (0..19) if any is not created
	 * @return
	 */
	public static String getFirstValidClientFolder(){
		return getFirstValidClientFolder(SharedData.getFilePath() + File.separator); 
	}
	
	static final int MIN_CLIENT_FOLDER_ID = 0;
	static final int MAX_CLIENT_FOLDER_ID = 10;
	
	/**
	 * runs trough basefolder on sdcard and checks which folder (0..19) if any is not created
	 * @param baseFolder must contain '\'
	 * @return
	 */
	public static String getFirstValidClientFolder(String baseFolder){
		File root = Environment.getExternalStorageDirectory();
		if (!root.canWrite()) {
			//SharedData.getInstance().addDebugDataLog_t("ERROR: cannot write to sdcard!");
			return null;
		}
		
		for(int folderID=MIN_CLIENT_FOLDER_ID ; folderID<=MAX_CLIENT_FOLDER_ID ; folderID++){
			File directory = new File(baseFolder + Integer.toString(folderID));
			if(!directory.exists()){
				return baseFolder + Integer.toString(folderID) + File.separator;
			}
		}
		
		return null;
	}


	/**
	 * runs trough basefolder on sdcard returns all valid clientfolder (does NOT check content)
	 * @return
	 */
	public static List<String> getAllClientFolders(){
		return getAllClientFolders(SharedData.getFilePath() + File.separator); 
	}
	
	
	/**
	 * runs trough basefolder on sdcard returns all valid clientfolder (does NOT check content)
	 * @param baseFolder must contain '\'
	 * @return
	 */
	public static List<String> getAllClientFolders(String baseFolder){
		List<String> clientFolders = new ArrayList<String>();
		for(int folderID=MIN_CLIENT_FOLDER_ID ; folderID<=MAX_CLIENT_FOLDER_ID ; folderID++ ){
			File directory = new File(baseFolder + Integer.toString(folderID));
			if(directory.exists()){
				clientFolders.add(baseFolder + Integer.toString(folderID) + File.separator);
			}
		}
		
		return clientFolders;
	}
	
	public static void DeleteRecursive(String fileOrDirectory) {
		File file = new File(fileOrDirectory);
		DeleteRecursive(file);
	}
	/**
	 * Deletes a file or a folder recursively
	 * @param fileOrDirectory
	 */
	public static void DeleteRecursive(File fileOrDirectory) {
	    if (fileOrDirectory.isDirectory())
	        for (File child : fileOrDirectory.listFiles())
	            DeleteRecursive(child);

	    fileOrDirectory.delete();
	}


}
